package com.moleq.fileclinet;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

import android.os.AsyncTask;
import android.os.Environment;

public class FileClinetTask extends AsyncTask<String, Integer, String>
{
	
	@Override
	protected String doInBackground(String... params)
	{
		while (true)
		{
			ServerSocket serverSocket = null;
			BufferedOutputStream bos = null;
			BufferedInputStream bis = null;
			try
			{
				serverSocket = new ServerSocket(6600);
				Socket socket = serverSocket.accept();
				InputStream inputStream = socket.getInputStream();

				bis = new BufferedInputStream(inputStream);
				bos = new BufferedOutputStream(new FileOutputStream(
						new File(Environment.getExternalStorageDirectory(),
								"Items.csv")));

				byte[] buf = new byte[1024];
				int len = bis.read(buf, 0, 1024);
				System.out.println("len =" + len);
				while (-1 != len)
				{
					bos.write(buf, 0, len);
					len = bis.read(buf);
				}
				bos.flush();
				bis.close();
				bos.close();

				inputStream.close();
				socket.close();
				serverSocket.close();
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}
}